#ifndef SATELLITECONTROLLER_H
#define SATELLITECONTROLLER_H

#include <vector>

class SvPlot;
class OrbitVirtualMachine;
class QTextEdit;

class SatelliteController
{
public:
    explicit SatelliteController(OrbitVirtualMachine *ovm) : m_ovm(ovm), m_plot(0), m_textEdit(0) {}
    void SolveHohmannTaskMinimizeFuel(int ScenarioID);
    void SolveHohmannTaskMaximizeFuel(int ScenarioID);
    void SolveMeetAndGreetTask(int ScenarioID);
    void SolveClearSkies(int ScenarioID);
    void SetPlot(SvPlot *plot){m_plot = plot;}
    void SetTextEdit(QTextEdit *textEdit){m_textEdit = textEdit;}
private:
    OrbitVirtualMachine *m_ovm;
    SvPlot *m_plot;
    QTextEdit *m_textEdit;
    std::vector<char> m_solution;
    int m_countPaint;
    int m_time;

    void Configure(int ScenarioID);
    double GetPortValue(int PortID);
    template <typename T> void Push(T Data);
    void PushHeader(int ScenarioID);
    void PushSpeed(double DeltaVx, double DeltaVy);
    void PushFinalFrame();
    void CalcParams(double sr, double tr,
                    double *v0, double *v1, double *t1);
    void CalcVElements(bool out, bool clockwise, double v, double sx, double sy, double sr,
                       double *vx, double *vy);
    void DoOneStep(int style);
    void DoManySteps(unsigned StepCount, int style);
    void StoreFile(int ScenarioID);
    bool needPaint();

    /// Utils
    double GetAngularSpeed(double x0, double y0, double x1, double y1, double sr, double time);
    double GetEarthAngle(double y, double r);
    double Distance(double dx, double dy);
    double Distance(double x1, double y1, double x2, double y2);
    double TranslateAngle(double angle);
    double GetEarthAngleTan(double x, double y);
    double GetAngularSpeedTan(double x0, double y0, double x1, double y1, double sr, double time);
    void FuncMG(double &sx1, double &sy1,
                                 double &tx1, double &ty1,
                                 double &sr,
                                 double &sphi1,
                                 double &tphi1);
    ///
    double HohmannTime(double targetR);
    void DoHohmann(double targetR,
                   bool checkClockWise = true, bool clockWise = false);
};

#endif // SATELLITECONTROLLER_H
